package com.eveinfo.api.character.list;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.eveinfo.api.AbstractApiHandler;
import com.eveinfo.api.ApiException;

public class CharacterListHandler extends AbstractApiHandler<CharacterListResponse> {

	public CharacterListHandler() {
		super();
		response = new CharacterListResponse();
	}
	
	public CharacterListResponse getParsedData() throws ApiException {
		if(response == null)
			throw new ApiException(error);
		return response;
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes attributes) throws SAXException {
		super.startElement(namespaceURI, localName, qName, attributes);
		String value = (!localName.equals("")) ? localName : qName;
		if (value.equals("row")) {
			ApiCharacter character = new ApiCharacter();
			String attrValue;
			attrValue = attributes.getValue("name");
			character.setName(attrValue);
			attrValue = attributes.getValue("characterID");
			character.setCharacterID(Integer.parseInt(attrValue));
			attrValue = attributes.getValue("corporationName");
			character.setCorporationName(attrValue);
			attrValue = attributes.getValue("corporationID");
			character.setCorporationID(Integer.parseInt(attrValue));
			response.addEveCharacter(character);
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
	}

}
